package com.gress.shop.controller.chart;

import com.gress.shop.annotation.FireLogger;
import com.gress.shop.bo.LogInfo;
import com.gress.shop.dao.ILogInfoDao;
import com.gress.shop.query.LogQuery;
import com.gress.shop.util.DateUtil;
import com.gress.shop.vo.ResultMap;
import com.gress.shop.vo.SwalReturn;
import com.wordnik.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created xiongwen with 2018/3/13 15:47
 * Description:
 */
@Controller
@RequestMapping("log")
@Api("日志")
public class AccessLogController {

    @Resource
    private ILogInfoDao dao;

    @RequestMapping("to_log")
    @FireLogger("日志")
    public String toHtml5() {
        return "/chart/log";
    }

    @RequestMapping("list")
    @ResponseBody()
    @FireLogger("动态查询日志")
    public ResultMap queryALlLog(LogQuery logQuery){
        assembleQuery(logQuery);
        return new ResultMap(dao.query(logQuery), dao.count(logQuery));
    }

    /**
     * @param logQuery
     * @Date: 15:28 2018/4/7
     * @Description: 进行初始化query
     * @return:
     */
    private void assembleQuery(LogQuery logQuery) {
        if (StringUtils.isEmpty(logQuery.getCreateBy())) {
            logQuery.setCreateBy(null);
        }
        if (StringUtils.isEmpty(logQuery.getModule())) {
            logQuery.setModule(null);
        }
        if (StringUtils.isEmpty(logQuery.getOpType())) {
            logQuery.setOpType(null);
        }
        if (!StringUtils.isEmpty(logQuery.getEnd())) {
            Long end = DateUtil.dateFormat_stringToLong(logQuery.getEnd(), DateUtil.DEFAULT_DATE_FORMAT);
            logQuery.setEndTime(end);
        }
        if (!StringUtils.isEmpty(logQuery.getStart())) {
            Long start = DateUtil.dateFormat_stringToLong(logQuery.getStart(), DateUtil.DEFAULT_DATE_FORMAT);
            logQuery.setStartTime(start);
        }
    }


    @RequestMapping("query_id")
    @ResponseBody()
    @FireLogger("查询某一条记录信息")
    public SwalReturn<LogInfo> queryId(String id){
        return new SwalReturn<>(dao.getById(id));
    }

    @RequestMapping("delete_id")
    @ResponseBody()
    @FireLogger("删除一行日志")
    public SwalReturn deleteId(String id){
        dao.deleteById(id);
        return new SwalReturn<>();
    }

}
